<template>
  <view>
    <my-goods v-for="item in goodsList" :key="item.goods_id" :goods="item" />
  </view>
</template>

<script>
export default {
  data () {
    return {
      queryObj: {
        query: '',
        cid: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0,
      isLoading: false
    }
  },
  onLoad (options) {
    this.queryObj.query = options.query || ''
    this.queryObj.cid = options.cid || ''

    this.getGoodsListData()
  },
  // 下拉刷新
  onPullDownRefresh () {
    this.queryObj.pagenum = 1
    this.goodsList = []

    this.getGoodsListData(true)
  },
  // 上拉加载更多
  onReachBottom () {
    if (this.isLoading) return

    // 判断是否还有数据
    if (this.goodsList.length >= this.total) {
      uni.showToast({
        title: '没有更多数据了~',
        icon: 'none'
      })

      return
    }
    this.queryObj.pagenum += 1
    this.getGoodsListData(false)
  },
  methods: {
    async getGoodsListData (isPullDownRefresh = false) {
      this.isLoading = true
      const res = await uni.$request({
        url: 'goods/search',
        data: this.queryObj,
        isPullDownRefresh
      })

      if (res.meta.status === 200) {
        this.isLoading = false
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      }
    }
  }
}
</script>
